#include "global.h"

const QString USER_DATA_PATH = "server_data.db";

QDataStream &operator<<(QDataStream &out, const Header &h)
{
    out << static_cast<int>(h);
    return out;
}

QDataStream &operator>>(QDataStream &in, Header &header)
{
    int headerInt;
    in >> headerInt;
    header = static_cast<Header>(headerInt);
    return in;
}

// QDataStream 写操作符
QDataStream &operator<<(QDataStream &out, const User &u) {
    out << u.id << u.ip << u.user_name << u.user_password << static_cast<int>(u.user_status);
    return out;
}

// QDataStream 读操作符
QDataStream &operator>>(QDataStream &in, User &u) {
    int status;
    in >> u.id >> u.ip >> u.user_name >> u.user_password >> status;
    u.user_status = static_cast<UserStatus>(status);
    return in;
}


QDataStream &operator<<(QDataStream &out, const Friend &f)
{
    out << f.id << f.friend_id << static_cast<int>(f.friend_status);
    return out;
}

QDataStream &operator>>(QDataStream &in, Friend &f)
{
    int status;
    in >> f.id >> f.friend_id >> status;
    f.friend_status = static_cast<UserStatus>(status);
    return in;
}

QDataStream &operator<<(QDataStream &out, const ChatHistory &chat) {
    out << chat.user_one_id << chat.user_two_id << chat.message
        << chat.message_time << static_cast<int>(chat.message_type);
    return out;
}

QDataStream &operator>>(QDataStream &in, ChatHistory &chat) {
    int type;
    in >> chat.user_one_id >> chat.user_two_id >> chat.message
        >> chat.message_time >> type;
    chat.message_type = static_cast<MessageType>(type);
    return in;
}
